<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Common;

use Srv\Apps\Apis\Common\Ctl\StorageCtl;
use Srv\Apps\Apis\Common\DefCtl\DefStorageCategoryCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Common\StorageGsDTO;
use Srv\Apps\Proto\DTO\Common\StorageStsDTO;
use Srv\Apps\Proto\Enum\Common\StorageSignUrlEnum;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Common\StorageGsListVO;
use Srv\Apps\Proto\VO\Common\StorageStsListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class StorageAct extends ActionAbstract
{
    private array $extNameAllowList     = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'ttf', 'mp3', 'mp4'];    // 存储媒体允许的后缀[小写]

    /**
     * @url /basic/storage/sts/get
     * @auth getStorageSTS
     * @flag HIDE, SKIP
     * @req Srv.Apps.Proto.DTO.Common.StorageStsDTO
     * @rsp Srv.Apps.Proto.VO.Common.StorageStsListVO
     * 获取云存储STS
     */
    public function getStorageSTS(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StorageStsDTO) || !($VOClass instanceof StorageStsListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $extNameList        = $DTOClass->toArrayList($DTOClass->getExtNameList());;
        $LogCtl             = LogCtl::tag();
        $logData            = $RouteConfData->getParamsResData($DTOClass);
        foreach ($extNameList as $extName){
            if(!in_array($extName, $this->extNameAllowList, true)){
                $errMsg     = '文件类型错误';
                $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
                return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
            }
        }
        $dsacId             = DefStorageCategoryCtl::tag()->getIdByKey($DTOClass->getTypeKey());
        if($dsacId < 1){
            $errMsg         = '存储类型Key错误';
            $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        }
        $admUserId          = AuthData::getLoginUserId();
        $StorageCtl         = StorageCtl::tag();
        $StorageStsListVO   = $StorageCtl->getStorageSTS($admUserId, $dsacId, $extNameList, StorageCtl::GROUP_NAME_STS_DEF);
        if(!($StorageStsListVO instanceof StorageStsListVO)){
            $errMsg         = '获取云存储STS失败';
            $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::get(LangKeyEnum::LANG_FAILED));
        }
        $errMsg             = '获取云存储STS成功';
        $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', $StorageStsListVO, []);
    }

    /**
     * @url /basic/storage/gs/get
     * @auth basicStorageGsGet
     * @req Srv.Apps.Proto.DTO.Common.StorageGsDTO
     * @rsp Srv.Apps.Proto.VO.Common.StorageGsListVO
     * 获取云存储GS
     */
    public function getStorageGS(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StorageGsDTO) || !($VOClass instanceof StorageGsListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $extNameList        = $DTOClass->toArrayList($DTOClass->getExtNameList());;
        $LogCtl             = LogCtl::tag();
        $logData            = $RouteConfData->getParamsResData($DTOClass);
        foreach ($extNameList as $extName){
            if(!in_array($extName, $this->extNameAllowList, true)){
                $errMsg     = '文件类型错误';
                $LogCtl->logAdd(LogCtl::STORAGE_GS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
                return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
            }
        }
        $dsacId             = DefStorageCategoryCtl::tag()->getIdByKey($DTOClass->getTypeKey());
        if($dsacId < 1){
            $errMsg         = '存储类型Key错误';
            $LogCtl->logAdd(LogCtl::STORAGE_GS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        }
        $StorageCtl         = StorageCtl::tag();
        $extDataJson        = $DTOClass->getExtData();
        $signUrlStatus      = StorageSignUrlEnum::parseValue($DTOClass->getSignUrlStatus());
        $userId             = AuthData::getLoginUserId();
        $groupName          = $StorageCtl->isStorageVod($DTOClass->getTypeKey()) ? StorageCtl::GROUP_NAME_SGS_VOD : StorageCtl::GROUP_NAME_SGS_DEF;
        $StorageGsListVO    = $StorageCtl->getStorageGS($userId, $dsacId, $extNameList, $signUrlStatus, $extDataJson, $groupName);
        if(!($StorageGsListVO instanceof StorageGsListVO)){
            $errMsg         = '获取云存储GS失败';
            $LogCtl->logAdd(LogCtl::STORAGE_GS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::get(LangKeyEnum::LANG_FAILED));
        }
        $errMsg             = '获取云存储GS成功';
        $LogCtl->logAdd(LogCtl::STORAGE_GS_GET, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', $StorageGsListVO, []);
    }

    /**
     * @url /basic/storage/sts/dir/get
     * @auth getStorageSTSAtDir
     * @flag HIDE, SKIP
     * @req Srv.Apps.Proto.DTO.Common.StorageStsDTO
     * @rsp Srv.Apps.Proto.VO.Common.StorageStsListVO
     * 获取云存储STS-目录授权
     */
    public function getStorageSTSAtDir(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StorageStsDTO) || !($VOClass instanceof StorageStsListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $extNameList        = $DTOClass->toArrayList($DTOClass->getExtNameList());;
        $LogCtl             = LogCtl::tag();
        $logData            = $RouteConfData->getParamsResData($DTOClass);
        foreach ($extNameList as $extName){
            if(!in_array($extName, $this->extNameAllowList, true)){
                $errMsg     = '文件类型错误';
                $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
                return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
            }
        }
        $dsacId             = DefStorageCategoryCtl::tag()->getIdByKey($DTOClass->getTypeKey());
        if($dsacId < 1){
            $errMsg         = '存储类型Key错误';
            $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        }
        $StorageCtl         = StorageCtl::tag();
        $admUserId          = AuthData::getLoginUserId();
        $StorageStsListVO   = $StorageCtl->getStorageSTSAtDir($admUserId, $dsacId, $extNameList, StorageCtl::GROUP_NAME_STS_DEF);
        if(!($StorageStsListVO instanceof StorageStsListVO)){
            $errMsg         = '获取云存储STS失败';
            $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::get(LangKeyEnum::LANG_FAILED));
        }
        $errMsg             = '获取云存储STS成功';
        $LogCtl->logAdd(LogCtl::STORAGE_STS_GET, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', $StorageStsListVO, []);
    }
}